<?php
namespace app\blog\controller;
use cmf\controller\BaseController;
use think\Db;
//定时任务
class TimerController extends BaseController
{
    public function total(){      
        //店铺营业额汇总
        //今天0点时间
        $today_time=strtotime('today');        
        $check=Db::name('TotalStatis')->where(array('ctime'=>array('egt',$today_time)))->find();/* 判断今天有没有汇总 */
        if($check){
            exit;
        }
         //昨天0点时间
        $yesterday_time=strtotime('yesterday');
        //判断不能重复汇总
        $list_store_list=Db::name('Store')->where(array('status'=>1))->select()->toarray();//正在运营的店铺列表
        $map['ctime'][]=array('gt',$yesterday_time);
        $map['ctime'][]=array('lt',$today_time);
        $map['paystatus']=1;//已付款
        //如果当天补充流水里面有
        $con['ctime'][]=array('gt',$yesterday_time);
        $con['ctime'][]=array('lt',$today_time);        
        foreach ($list_store_list as $k=>$v){
            $map['storeid']=$v['id'];
            $con['storeid']=$v['id'];
            $order_money=Db::name('Order')->where($map)->sum('actualMoney');
            $order_li=Db::name('Order')->where($map)->select()->toarray();
            $supply_money=Db::name('Supply')->where($con)->sum('money');  
            if($order_money>0){
                $total_money=$order_money+$supply_money;
                //把记录存数据库
                $data=array(
                    'storeid'=>$v['id'],
                    'money'=>$total_money,
                    'ctime'=>time()
                );
                $re=Db::name('TotalStatis')->insert($data);
            }
        }
    }        
    public function single(){
        //单品/套餐营业额汇总
        //今天0点时间
        $today_time=strtotime('today');
        //昨天0点时间
        $yesterday_time=strtotime('yesterday');
        $check=Db::name('SingleStatis')->where(array('ctime'=>array('egt',$today_time)))->find();/* 判断今天有没有汇总 */
        if($check){
            exit;
        }
        $list_store_list=Db::name('Store')->where(array('status'=>1))->select()->toarray();//正在运营的店铺列表        
        foreach ($list_store_list as $k=>$v){
            //通过店铺列表获取每个店铺的商品id
            $gid_arr=array();
            $gid_list=Db::name('Goods')->where(array('g_status'=>1,'storeid'=>$v['id']))->field('g_id')->select()->toarray();
            foreach ($gid_list as $gk=>$gv)
            {
                $gid_arr[]=$gv['g_id'];
            }
            foreach ($gid_arr as $kg=>$vg){
                $map['ctime']=array('gt',$yesterday_time);
                $map['ctime']=array('lt',$today_time);
                $map['paystatus']=1;//已付款
                $map['storeid']=$v['id'];
                $map['goodsid']=$vg;
                $map['stemeal']=0; 
                $total_money=Db::name('OrderDetail')->where($map)->sum('money');//该店铺该商品总营业额
                $num=Db::name('OrderDetail')->where($map)->count();////该店铺该商品总销售数量 
                if(!empty($total_money) || !empty($num)){
                    //把记录存数据库
                    $data=array(
                        'gid'=>$vg,
                        'storeid'=>$v['id'],
                        'money'=>$total_money,
                        'num'=>$num,
                        'type'=>1,
                        'ctime'=>time()
                    );
                    $re=Db::name('SingleStatis')->insert($data);
                }
            }
            //获取套餐列表id
            $setmeal_list=Db::name('Setmeal')->where(array('set_shopid'=>$v['id'],'set_end'=>array('gt',$yesterday_time)))->select()->toarray();             
            foreach ($setmeal_list as $ks=>$vs){
                $con['ctime']=array('gt',$yesterday_time);
                $con['ctime']=array('lt',$today_time);
                $con['paystatus']=1;//已付款
                $con['storeid']=$v['id'];
                $con['goodsid']=$vs['set_id'];
                $con['stemeal']=1;
                $total_moneys=Db::name('OrderDetail')->where($con)->sum('money');//数量
                $nums=Db::name('OrderDetail')->where($con)->count();
                //把记录存数据库
                $datas=array(
                    'storeid'=>$v['id'],
                    'money'=>$total_moneys,
                    'num'=>$nums,
                    'type'=>2,
                    'ctime'=>time(),
                    'gid'=>$vs['set_id']
                );
                $re=Db::name('SingleStatis')->insert($datas);
            }    
        }
    }
    public function operator(){
        //今天0点时间
        $today_time=strtotime('today');
        //昨天0点时间
        $yesterday_time=strtotime('yesterday');
        $check=Db::name('OperatorStatis')->where(array('ctime'=>array('egt',$today_time)))->find();/* 判断今天有没有汇总 */
        if($check){
           exit;
        }     
        $operator_list=Db::name('User')->where(array('store_id'=>array('neq','0'),'status'=>1))->select()->toarray();
        //echo Db::name('User')->getlastsql();exit;
        foreach($operator_list as $k=>$v){
            $con['operatorid']=$v['id'];
            $con['ctime']=array('gt',$yesterday_time);
            $con['ctime']=array('lt',$today_time);
            $con['payStatus']=1;
            $totalMoney=Db::name('Order')->where($con)->sum('totalMoney');//销售金额
            $actualMoney=Db::name('Order')->where($con)->sum('actualMoney');//付款金额
            $getMoney=Db::name('Order')->where($con)->sum('getMoney');//实收金额
            $giveChange=Db::name('Order')->where($con)->sum('giveChange');//找零金额
            $discount=Db::name('Order')->where($con)->sum('discount');//优惠金额
            //补充流水】
            $where['operatorid']=$v['id'];
            $where['ctime']=array('gt',$yesterday_time);
            $where['ctime']=array('lt',$today_time);
            $supply_money=Db::name('Supply')->where($where)->sum('money');
           if($actualMoney){
               $datas=array(
                   'storeid'=>$v['store_id'],
                   'operatorid'=>$v['id'],
                   'totalMoney'=>$totalMoney,
                   'actualMoney'=>$actualMoney,
                   'getMoney'=>$getMoney,
                   'giveChange'=>$giveChange,
                   'discount'=>$discount,
                   'ctime'=>time(),
               );
               $re=Db::name('OperatorStatis')->insert($datas);
           }
        }   
    }
}
